<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_UserLocationController extends Zend_Rest_Controller
{
	public function init()
	{
		$this->_helper->viewRenderer->setNoRender(true);
	}


	public function deleteAction() {

	}

	public function getAction() {

	}

	public function headAction() {

	}

	public function indexAction() {
		try{

			$userlocation_mapper = new Api_Model_UserLocationMapper();

			//lấy user_id
			$token = $this->getRequest()->getHeader('token');
			$user = $userlocation_mapper->getuserid($token);

			$userlocation = $userlocation_mapper->getUserLocation($user['user_id']);
			$this->getresponse()->appendBody(json_encode($userlocation));
			$this->getResponse()->setHttpResponseCode(200);

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function postAction() {
		try{
			$userlocation_mapper = new Api_Model_UserLocationMapper();

			//lấy user_id
			$token = $this->getRequest()->getHeader('token');
			$user = $userlocation_mapper->getuserid($token);


			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);


			if ($data[longitude] != "" || $data[latitude] != ""){

				$userlocation_mapper->postUserLocation($data, $user['user_id']);
				throw new Exception(json_encode(array('meta' => 'Created')),201);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function putAction() {

	}
}

